package com.ts.api.module.match.plan;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.func.FuncFilter;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.module.match.plan.handler.ApiPlanMatchProcessFailureHandler;
import com.ts.api.module.match.plan.handler.ApiPlanMatchProcessSuccessHandler;
import com.ts.api.module.match.plan.handler.ChannelPlanMatchProcessSuccessHandler;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeResult;
import com.ts.api.module.match.plan.internal.executor.handler.PlanExecutorHandler;
import com.ts.api.module.match.plan.internal.qualification.handler.PlanQualificationHandler;
import com.ts.api.module.match.plan.internal.qualification.model.PlanQualificationResult;
import com.ts.api.module.match.plan.model.AbstractPlanMatch;
import com.ts.api.module.match.plan.model.PlanMatchProcess;
import com.ts.api.module.product.entity.po.ProductPlanPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  计划撞库
 *
 *  @author Pan
 */
@Service
public class PlanDataExchangeFacade {

    @Autowired
    private PlanQualificationHandler planQualificationHandler;
    @Autowired
    private PlanExecutorHandler planExecutorHandler;
    @Autowired
    private ApiPlanMatchProcessFailureHandler apiPlanMatchProcessFailureHandler;
    @Autowired
    private ApiPlanMatchProcessSuccessHandler apiPlanMatchProcessSuccessHandler;
    @Autowired
    private ChannelPlanMatchProcessSuccessHandler channelPlanMatchProcessSuccessHandler;

    /**
     *  搜索可分发的计划列表
     *
     *  @param  abstractPlanMatch   计划匹配抽象实体
     *  @return PlanQualificationResult
     */
    public PlanQualificationResult searchDistributePlan(AbstractPlanMatch abstractPlanMatch) {
        //  过滤计划资与质用户资质，返回符合条件的计划列表
        return planQualificationHandler.handleFilter(abstractPlanMatch);
    }

    /**
     *  搜索可分发的计划列表
     *  自定义二次过滤
     *
     *  @param  abstractPlanMatch   计划匹配抽象实体
     *  @return PlanQualificationResult
     */
    public PlanQualificationResult searchDistributePlan(AbstractPlanMatch abstractPlanMatch, FuncFilter<ProductPlanPO> exclude) {
        //  过滤计划资与质用户资质，返回符合条件的计划列表
        PlanQualificationResult planQualificationResult = planQualificationHandler.handleFilter(abstractPlanMatch);

        List<ProductPlanPO> listPlanQualification = planQualificationResult.getListPlanQualification();
        if (ValidParam.isEmpty(listPlanQualification)) {
            return planQualificationResult;
        }

        //  自定义二次过滤
        List<ProductPlanPO> copy = CollUtils.copy(listPlanQualification);
        CollUtils.filter(copy, exclude);
        planQualificationResult.setListPlanQualification(copy);

        return planQualificationResult;
    }

    /**
     *  自有渠道-撞库匹配/MD5模式/掩码模式
     *
     *  @param  planQualificationResult   计划资质校验通过集合
     *  @return PlanDistributeResult      计划匹配返回对象
     */
    public PlanDistributeResult channelDataExchange(PlanQualificationResult planQualificationResult) {
        //  执行计划分发-获取执行计划实体
        List<PlanDistributeResult> listMatchResult = planExecutorHandler.execute(planQualificationResult);

        //  失败默认处理
        if (ValidParam.isEmpty(listMatchResult)) {
            return PlanDistributeResult.ofFail("无计划分发列表");
        }

        //  构建计划匹配处理数据实体
        PlanMatchProcess planMatchProcess = PlanMatchProcess.of(planQualificationResult, listMatchResult);
        return channelPlanMatchProcessSuccessHandler.handleSuccess(planMatchProcess);
    }

    /**
     *  API渠道-撞库匹配/MD5模式
     *
     *  @param  planQualificationResult   计划资质校验通过集合
     *  @return PlanDistributeResult      计划匹配返回对象
     */
    public PlanDistributeResult apiDataExchange(PlanQualificationResult planQualificationResult) {
        //  执行计划分发-获取执行计划实体
        List<PlanDistributeResult> listMatchResult = planExecutorHandler.execute(planQualificationResult);

        //  构建计划匹配处理数据实体
        PlanMatchProcess planMatchProcess = PlanMatchProcess.of(planQualificationResult, listMatchResult);
        if (ValidParam.isEmpty(listMatchResult)) {
            return apiPlanMatchProcessFailureHandler.handleFailure(planMatchProcess);
        }
        return apiPlanMatchProcessSuccessHandler.handleSuccess(planMatchProcess);
    }

    /**
     *  API渠道-撞库匹配/手机号掩码模式
     *
     *  @param  planQualificationResult   计划资质校验通过集合
     *  @return PlanDistributeResult      计划匹配返回对象
     */
    public List<PlanDistributeResult> apiPhoneMaskDataExchange(PlanQualificationResult planQualificationResult) {
        //  执行计划分发-获取执行计划实体
        List<PlanDistributeResult> listMatchResult = planExecutorHandler.execute(planQualificationResult);

        //  构建计划匹配处理数据实体
        PlanMatchProcess planMatchProcess = PlanMatchProcess.of(planQualificationResult, listMatchResult);
        if (ValidParam.isEmpty(listMatchResult)) {
            return apiPlanMatchProcessFailureHandler.handlePhoneMaskFailure(planMatchProcess);
        }
        return apiPlanMatchProcessSuccessHandler.handlePhoneMaskSuccess(planMatchProcess);
    }
}
